package com.zzy.database.controller;

import com.zzy.database.pojo.Account;
import com.zzy.database.pojo.Result;
import com.zzy.database.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/accounts")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @PostMapping
    public Result openAccount(@RequestBody Map<String, Object> requestData) {
        try {
            // 调试日志
            System.out.println("收到请求数据: " + requestData);

            Account account = new Account();
            account.setCustomer_id((String) requestData.get("customer_id"));
            account.setBranch_id((String) requestData.get("branch_id"));
            account.setAccount_type((String) requestData.get("account_type"));
            account.setBalance(Double.parseDouble(requestData.get("balance").toString()));

            accountService.openAccount(account);
            return Result.success("开户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping
    public Result deleteAccount(@RequestBody Account account) {
        try {
            accountService.deleteAccount(account.getAccount_id());
            return Result.success("账户删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/getInfo")
    public Result getAccountInfo(@RequestParam("account_id") String accountId) {
        Account result = accountService.getAccountInfo(accountId);
        if (result == null) {
            return Result.error("账户不存在");
        } else {
            return Result.success(result);
        }
    }

    @GetMapping("/getAll")
    public Result getAllAccounts() {
        List<Account> accountList = accountService.getAllAccounts();
        return Result.success(accountList);
    }
    @GetMapping("/getByCustomerId")
    public Result getAccountsByCustomerId(@RequestParam("customer_id") String customerId) {
        List<Account> accountList = accountService.getAccountsByCustomerId(customerId);
        return Result.success(accountList);
    }

    @PutMapping("/updateBalance")
    public Result updateBalance(@RequestBody Account account) {
        try {
            accountService.updateBalance(account.getAccount_id(), account.getBalance());
            return Result.success("余额更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/getByBranchId")
    public Result getAccountsByBranchId(@RequestParam("branch_id") String branchId) {
        List<Account> accountList = accountService.getAccountsByBranchId(branchId);
        return Result.success(accountList);
    }

    @GetMapping("/getTotalBalanceByBranch")
    public Result getTotalBalanceByBranch(@RequestParam("branch_id") String branchId) {
        double totalBalance = accountService.getTotalBalanceByBranch(branchId);
        return Result.success(totalBalance);
    }
}